package com.seecen.controller;


import com.seecen.pojo.User;
import com.seecen.service.UserService;
import com.seecen.utils.JwtHelper;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private UserService userService;

    @PostMapping("login")
    private Result login(@RequestBody User user){
        Result ret = userService.login(user);
        return ret;
    }

    @GetMapping("getUserInfo")
    private Result getUserInfo(@RequestHeader String token){
        Result ret = userService.getUserInfo(token);
        return ret;
    }


    @PostMapping("register")
    private Result register(@RequestBody User user){
        Result ret = userService.register(user);
        return ret;
    }

    @PostMapping("checkUserName")
    private Result checkUserName(@RequestParam("username") String username){
        Result ret = userService.checkUserName(username);
        return ret;
    }

    @GetMapping("checkLogin")
    private Result checkLogin(@RequestHeader String token) {
        boolean isExpiration = jwtHelper.isExpiration(token);
        if(isExpiration){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }
}
